package net.tngou.iask.action.my;

import java.util.List;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Advice;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.service.AdviceService;
import net.tngou.iask.service.QuestionService;

/**
 * 问题建议
 * @author tngou@tngou.net
 *
 */
public class AdviceAction extends BaseAction {

	/**
	 * 建议列表
	 */
	public void list() {
		
		AdviceService service = new AdviceService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), ask.getKeyword());
		root.put("page", page);
		printFreemarker("my/advice_list.ftl", root);
	}
	
	/**
	 * 添加建议
	 */
	public void add() {
		if (ask.getId()==0) {
			run_404();return;
		}	
		Question question = new Question().get(ask.getId());
		if(question==null){run_404();return;}
		
		if(question.getType()==1||question.getType()==2)
		{
			QuestionService service = new QuestionService();
			List<Answer> list = service.getAnswer(question.getId());
			root.put("list", list);
		}
		root.put("question", question);	
		printFreemarker("my/advice_add.ftl", root);
	}
	
	/**
	 * 问题列表
	 */
	public void question() {
		
		QuestionService service = new QuestionService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), ask.getKeyword());
		root.put("page", page);
		printFreemarker("my/advice_question.ftl", root);
	}
	
	/**
	 * 更新建议
	 */
	public void update() {
		if (ask.getId()==0) {
			run_404();return;
		}
		Advice advice = new Advice().get(ask.getId());
		if(advice==null||advice.getMain()!=user.getMain())
		{
			run_404();return;
		}
		Question question = new Question().get(advice.getQuestion());
		if(question==null){run_404();return;}
		
		if(question.getType()==2||question.getType()==3)
		{
			QuestionService service = new QuestionService();
			List<Answer> list = service.getAnswer(question.getId());
			root.put("list", list);
		}
		root.put("question", question);
		root.put("advice", advice);		
		printFreemarker("my/advice_update.ftl", root);
	}
}
